<?php
include("./config.php");
include("./utils.php");

$op = $_POST['op'];
$appid=$_POST['appid'];
$id=$_POST['id'];
$value = "";
if (isset($_POST['value'])){
  $value = $_POST['value'];
}

function get_sql($op, $appid, $id, $value){
  $sql = "";
  $appid = safe($appid);
  $id = safe($id);
  $value = safe($value);
  switch($op){
    # -------------------------------------------------------
  #case 'del-item':
  #  $sql = "DELETE FROM `item` WHERE `appid`=$appid AND `shortcutid`=$id LIMIT 1";
  #  break;

  # -------------------------------------------------------
  # 保存快捷键信息
  case 'save-des':
    $sql = "UPDATE `shortcut` SET `des`='$value' WHERE `appid`=$appid AND `shortcutid`=$id LIMIT 1";
    break;
  case 'save-name':
    $sql = "UPDATE `shortcut` SET `name`='$value' WHERE `appid`=$appid AND `shortcutid`=$id LIMIT 1";
    break;
  case 'save-skin':
    $sql = "UPDATE `shortcut` SET `skin`=$value WHERE `appid`=$appid AND `shortcutid`=$id LIMIT 1";
    break;
  case 'save-shortcut':
    $sql = "UPDATE `shortcut` SET `shortcut`='$value' WHERE `appid`=$appid AND `shortcutid`=$id LIMIT 1";
    break;
  case 'save-name-en':
    $sql = "UPDATE `shortcut` SET `name_en`='$value' WHERE `appid`=$appid AND `shortcutid`=$id LIMIT 1";
    break;
  case 'save-des-en':
    $sql = "UPDATE `shortcut` SET `des_en`='$value' WHERE `appid`=$appid AND `shortcutid`=$id LIMIT 1";
    break;
  case 'save-context':
    $sql = "UPDATE `shortcut` SET `context`='$value' WHERE `appid`=$appid AND `shortcutid`=$id LIMIT 1";
    break;
  case 'save-context-en':
    $sql = "UPDATE `shortcut` SET `context_en`='$value' WHERE `appid`=$appid AND `shortcutid`=$id LIMIT 1";
    break;

  # -------------------------------------------------------
  # 保存软件
  case 'save-app-logo':
    $sql = "UPDATE `app` SET `logo`='$value' WHERE `appid`=$appid LIMIT 1";
    break;
  case 'save-app-des':
    $sql = "UPDATE `app` SET `des`='$value' WHERE `appid`=$appid LIMIT 1";
    break;
  case 'save-app-name':
    $sql = "UPDATE `app` SET `name`='$value' WHERE `appid`=$appid LIMIT 1";
    break;
  case 'save-app-skin':
    $sql = "UPDATE `app` SET `skin`=$value WHERE `appid`=$appid LIMIT 1";
    break;
  case 'save-app-name-en':
    $sql = "UPDATE `app` SET `name_en`='$value' WHERE `appid`=$appid LIMIT 1";
    break;
  case 'save-app-des-en':
    $sql = "UPDATE `app` SET `des_en`='$value' WHERE `appid`=$appid LIMIT 1";
    break;
  case 'save-shortcut-order':
    $sql = "UPDATE `app` SET `shortcut_order`='$value' WHERE `appid`=$appid LIMIT 1";
    break;
  # -------------------------------------------------------
  case 'add-app':
    # 插入软件
    $userid = safe($_POST['userid']);
    $logo = safe($_POST['logo']);
    $des = safe($_POST['des']);
    $name = safe($_POST['name']);
    $skin = safe($_POST['skin']);
    $name_en = safe($_POST['name_en']);
    $des_en = safe($_POST['des_en']);
    $sql = "INSERT INTO `app`(`name`, `des`, `name_en`, `des_en`, `skin`, `logo`) VALUES('$name', '$des', '$name_en', '$des_en', '$skin', '$logo')";
    break;
  case 'add-shortcut':
    # 插入软件
    $userid = safe($_POST['userid']);
    $appid = safe($_POST['appid']);
    $name = safe($_POST['name']);
    $des = safe($_POST['des']);
    $shortcut = safe($_POST['shortcut']);
    $skin = safe($_POST['skin']);
    $name_en = safe($_POST['name_en']);
    $des_en = safe($_POST['des_en']);
    $context = safe($_POST['context']);
    $context_en = safe($_POST['context_en']);
    $sql = "INSERT INTO `shortcut`(`appid`, `shortcut`, `name`, `des`, `context`, `name_en`, `des_en`, `context_en`, `skin`) VALUES('$appid', '$shortcut', '$name', '$des', '$context', '$name_en', '$des_en', '$context_en', '$skin')";
    break;
  case 'add-shortcuts':
    # 插入软件
    $userid = safe($_POST['userid']);
    $data = $_POST['data'];
    $appid = safe($_POST['appid']);
    if (!$appid){
      echo '{code:10002, msg:"no appid"}';
      exit(0);
    }

    $sql = 'INSERT INTO `shortcut`(`appid`, `shortcut`, `name`, `des`, `context`, `name_en`, `des_en`, `context_en`, `skin`) VALUES ';
    foreach($data as $k => $item) {
        $name = trim(safe($item['name']));
        $des = trim(safe($item['des']));
        $shortcut = trim(safe($item['shortcut']));
        if($shortcut=="" || $name==""){
          continue;
        }
        $skin = safe($item['skin']);
        $name_en = safe($item['name_en']);
        $des_en = safe($item['des_en']);
        $context = safe($item['context']);
        $context_en = safe($item['context_en']);
        $sql .= "('$appid', '$shortcut', '$name', '$des', '$context', '$name_en', '$des_en', '$context_en', '$skin'),";
    }
    $sql = rtrim($sql, ',');

    //// TODO
    //var_dump($_POST);
    //var_dump($sql);
    //$sql="";

    break;

  case 'add-sns':
    # 插入评论
    $userid = safe($_POST['userid']);
    $appid = safe($_POST['appid']);
    $shortcutid = safe($_POST['id']);
    $content = safe($_POST['content']);
    $status = safe($_POST['status']);
    $parent = safe($_POST['parent']);
    $sql = "INSERT INTO `sns`(`userid`, `appid`, `shortcutid`, `parent`, `content`, `status`) VALUES('$userid', '$appid', '$shortcutid', '$parent', '$content', '$status')";
    break;


  # -------------------------------------------------------
  # 评论相关
  case 'set-sns-status':
    # 添加评论的状态
    $id = safe($_POST['id']);
    $sql = "UPDATE `sns` SET `status=`status`|$status WHERE `id`='$id' LIMIT 1";
    break;
  # -------------------------------------------------------


  case 'set-info':
    $sql = "INSERT INTO `info`(`appid`, `id`, `value`) VALUES($appid, $id, $value) ON DUPLICATE KEY UPDATE `value`='$value'";
    break;
  case 'change-info':
    $sql = "INSERT INTO `info`(`appid`, `id`, `value`) VALUES($appid, $id, $value) ON DUPLICATE KEY UPDATE `value`=`value`+'$value'";
    break;
  case 'clear-info':
    $sql = "DELETE FROM `info` WHERE `id`=$id AND `appid`=$appid";
    break;
  case 'set-str':
    $sql = "INSERT INTO `str`(`appid`, `id`, `value`) VALUES($appid, $id, '$value') ON DUPLICATE KEY UPDATE `value`='$value'";
    break;


  default:
    break;
  }
  return $sql;

}


$sql = get_sql($op, $appid, $id, $value);
echo $sql;
var_dump($_POST);

$state = 0;
if($sql != ""){
  $state = mysql_query($sql);
  if (state && strpos($op, 'add')!=FALSE){
    $state = mysql_insert_id();
  }
  echo '\n{code:$state, msg:"success"}';
}else{
  echo '\n{code:1001,msg:"sql null"}';
}


?>

